<template>
	<el-container>
		<el-aside class="nopadding treesearch" style="width: 200px;background-color: #fff;" >
			<ex-search-tree ref="searchTree" :treeApi="this.$API.asset.card.treeData" @changeHandler="searchTreeHandler" :param="{isGroup:'1'}" :title="title">
					</ex-search-tree>
		</el-aside>
			<el-container>
				<el-header>
			<div class="left-panel">
				<fsButton ref="button" plain size="default" :buttons="buttons" @clickHandler="buttonHandler"></fsButton>
			</div>
			<div class="right-panel">
				<ex-search ref="search" :searches="searches" @searchOk="searchHandler" default-code="name" placeholder="请输入名称" :initParams="{isGroup:''}"></ex-search>
			</div>
		</el-header>
				<el-main class="nopadding">
					<ex-table id="cardTable" ref="table" :columns="columns" :url="this.$API.asset.card.list.url" :refs="this.$refs" search="search"
					@columnClick="columnClickHandler"
					@checkClick="checkClickHandler"></ex-table>
				</el-main>
			</el-container>
	</el-container>

	<save-dialog  ref="saveDialog" @success="searchHandler"></save-dialog>
	<view-dialog ref="viewDialog"/>

</template>

<script>
import saveDialog from './save.vue'
import viewDialog from './view'
import ExTable from "@/components/exTable/index.vue";
import ExSearch from "@/components/exSearch/index.vue";
import ExTreeNavigation from "@/components/exSearchTree/index.vue";
import ExSearchTree from "@/components/exSearchTree/index.vue";
import useTabs from "@/utils/useTabs";
import BaseTemplate from "@/utils/baseTemplate";
//import dict from "@/utils/dict";

export default {
	name: '卡片设计',
	components: {
		ExSearchTree,
		ExTreeNavigation,
		ExSearch,
		ExTable,
		saveDialog,
		viewDialog
	},

	data() {
		return {
			dialog: {
				save: false,
				permission: false
			},
			apiObj: [],
			buttons: [
        {name: this.$t('button.add'), icon: "el-icon-plus", click: "add", width:"70px",permission:"add"},
        {name: this.$t('button.edit'), icon: "el-icon-edit", type: "parent",parent:true,show:"none",width:"70px",
        childrenbuttons:[
         // {name: this.$t('button.edit'),  click: "edit",permission:"edit"},
          {name: this.$t('button.delete'),  click: "del",permission:"delete"},
          {name: this.$t('button.enabled'),  click: "enabled",permission:"enabled"},
          {name: this.$t('button.disabled'),  click: "disabled",permission:"disabled"},
		  {name: this.$t('复制'), click: "copy",permission: "copy"},
        ]},
        {name: "...", type: "parent",parent:true,permission:"export",
        childrenbuttons:[
          {name: this.$t('button.export'),  click: "export",permission:"export"},
        ]},
      ],
			searches: [
				{type: "input", name: "卡片名称", code: "name", placeholder: "请输入名称", maxlength: 30, showLimit: true},
				{type: "input", name: "卡片编码", code: "code", placeholder: "请输入编码", maxlength: 30, showLimit: true},
				{type: "radio", name: "是否分组", code: "isGroup",dictCode:'IS_GROUP'},
				// {type: "select", name: "资产大类", code: "mainCategoryCode",dictCode:'MAIN_CATEGORY'},
				//{type: "checkbox", name: "多选", code: "checkBoxType",ops: this.getOptionData()},
			],
			columns: [
				{code: "操作", name: "操作", width: "100",
				click:[{label:"",icon:"el-icon-edit-pen",event:'edit',permission: "edit"},
      {label:"",icon:"el-icon-delete",event:'detaildel',permission: "delete"}
				]},
				{code: "isGroup", name: "类型", width:"120",formatter:"DICT",key: "CARD_IS_GROUP"},
				// {code: "parentLevelCode", name: "所属组", width:"120",formatter:"DICT",key: "FamsAssetCard"},
				// {code: "code", name: "编码", width:"",sortable:true},
				{code: "name", name: "名称", width:"",click: "show1"},
				// {code: "mainCategoryCode", name: "资产大类", width:"80",formatter: "DICT",key: "MAIN_CATEGORY"},
				{code: "isEnabled", name: "是否可用", width:"100",sortable:true,formatter: "DICT",key: "IS_ENABLED"},
				// {code: "createUser", name: "创建人", width:"100",formatter: "DICT",key: "SysUser"},
				// {code: "createTime", name: "创建时间", width:"150",sortable:true,formatter: "DATETIME"},
			],
			parentLevelCode:"",
			title:"卡片设计",
		}
	},
	created() {
	},
	mounted() {
	},
	methods: {
		checkClickHandler(value) {
      if(value==0){
        for (let index = 0; index < this.buttons.length; index++) {
          if(this.buttons[index].show!=undefined){
            this.buttons[index].show="none"
          }
        }
      }else{
        for (let index = 0; index < this.buttons.length; index++) {
          if(this.buttons[index].show!=undefined){
          this.buttons[index].show=""
          }
        }
      }
    },
		buttonHandler(index, name) {
			this.$options.methods[name](this, index);
		},
		async searchTreeHandler(data) {
			let queryParams = {pathCode:data.levelCode}
			this.parentLevelCode=data.levelCode;
			await this.$refs.table.refresh(queryParams);
		},
		async searchHandler() {
			let queryParams = {pathCode: this.parentLevelCode}
			await this.$refs.table.refresh(queryParams);
		},
		columnClickHandler(name,data){
			this.$options.methods[name](this, data);
		},
		//添加
		add(_this) {
			let data = {
				parentLevelCode: _this.parentLevelCode,
				entityName:_this.$CONSTANT.entity.FamsAssetCard.name,
			};
			_this.$nextTick(() => {
				_this.$refs.saveDialog.open(data);
			})
		},
		//编辑
		async edit(_this,row) {
      let rsp = await _this.$API.asset.card.findById.post(row);
      if (rsp.code === 2000) {
        _this.$nextTick(() => {
          _this.$refs.saveDialog.open(rsp.data);
        })
      }
    },
		//查看
		show(_this) {
			let row = _this.$refs.table.selectionOne();
			if (!row) {
				return;
			}
			_this.$nextTick(() => {
				_this.$refs.viewDialog.open(row);
			})
		},
		//查看
		show1(_this,row) {
			_this.$nextTick(() => {
				_this.$refs.viewDialog.open(row);
			})
		},
		//导出
		async export(_this,index) {
      let attachParams = {};
      let exportApi = _this.$API.asset.card.export;
      let exTableRef = _this.$refs.table;
      let exportBtnRef = _this.$refs.button;
      let exportBtnIndex = index;
      await BaseTemplate.export(_this, exportApi, attachParams, exportBtnRef, exportBtnIndex, exTableRef);
		},
		//批量删除
		del(_this, index) {
			let selections = _this.$refs.table.selections();
			if (selections.length === 0) {
        _this.$message.warning(_this.$t("common.please_select_data"));
				return;
			}
			_this.$confirm(`确定删除选中的 ${selections.length} 项吗？如果删除项中含有子集将会被一并删除`, '警告', {
				type: 'warning'
			}).then(async () => {
				_this.$refs.button.loading(index, true);
				var delarray = [];
				for (let index = 0; index < selections.length; index++) {
					const element = selections[index];
					delarray.push(element.id);
				}
				var res = await _this.$API.asset.card.deleteByIds.post(delarray);
				if (res.code === 2000) {
					_this.$message.success(_this.$t("code."+res.code));
					_this.searchHandler();
					_this.$store.dispatch("commit",selections[0].entityName);
				}
				_this.$refs.button.loading(index, false);
			}).catch(() => {
				_this.$refs.button.loading(index, false);
			});
		},
		//单个删除
		detaildel(_this, row) {
			let selections = [];
			selections.push(row);
			_this.$confirm(`确定删除选中的 ${selections.length} 项吗？如果删除项中含有子集将会被一并删除`, '警告', {
				type: 'warning'
			}).then(async () => {
				var delarray = [];
				for (let index = 0; index < selections.length; index++) {
					const element = selections[index];
					delarray.push(element.id);
				}
				var res = await _this.$API.asset.card.deleteByIds.post(delarray);
				if (res.code === 2000) {
					_this.$message.success(_this.$t("code."+res.code));
					_this.searchHandler();
					_this.$store.dispatch("commit",selections[0].entityName);
				}
			}).catch(() => {
			});
		},
		//批量启用
		enabled(_this, index) {
			let selections = _this.$refs.table.selections();
			if (selections.length === 0) {
				_this.$message.warning(_this.$t("common.please_select_data"));
				return;
			}
			_this.$confirm(`确定启用选中的 ${selections.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				_this.$refs.button.loading(index, true);
				var delarray = [];
				for (let index = 0; index < selections.length; index++) {
					const element = selections[index];
					delarray.push(element.id);
				}
				var res = await _this.$API.asset.card.enabled.post(delarray);
				if (res.code === 2000) {
					_this.$message.success(_this.$t("code."+res.code));
					_this.searchHandler();
					_this.$store.dispatch("commit",selections[0].entityName);
				}
				_this.$refs.button.loading(index, false);
			}).catch(() => {
				_this.$refs.button.loading(index, false);
			});
		},
		//批量禁用
		disabled(_this, index) {
			let selections = _this.$refs.table.selections();
			if (selections.length === 0) {
				_this.$message.warning(_this.$t("common.please_select_data"));
				return;
			}
			_this.$confirm(`确定禁用选中的 ${selections.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				_this.$refs.button.loading(index, true);
				var delarray = [];
				for (let index = 0; index < selections.length; index++) {
					const element = selections[index];
					delarray.push(element.id);
				}
				var res = await _this.$API.asset.card.disabled.post(delarray);
				if (res.code === 2000) {
					_this.$message.success(_this.$t("code."+res.code));
					_this.searchHandler();
					_this.$store.dispatch("commit",selections[0].entityName);
				}
				_this.$refs.button.loading(index, false);
			}).catch(() => {
				_this.$refs.button.loading(index, false);
			});
		},
		//编辑
		async copy(_this, index) {
			let row = _this.$refs.table.selectionOne();
			if (!row) {
				return;
			}
			_this.$refs.button.loading(index, true);
			let rsp = await _this.$API.asset.card.copy.post(row);
			if (rsp.code === 2000) {
				useTabs.refresh();
			}
			_this.$refs.button.loading(index, false);
		},
	}
}
</script>
